#include "appcontroller.h"
#include "hotkeymapper.h"
#include "ocrclient.h"
#include <QFuture>
#include <QThread>
#include <QtConcurrent>
#include <QMovie>
#include <QLabel>

AppController::AppController(QObject *parent)
    : QObject{parent}
{
    m_loginWidget = new LoginOrSignUpWidget();
    m_loginWidget->setWindowIcon(QIcon(":/icons/cat.jpg"));
    m_splash = new CSplashScreen(":/icons/loading加载中_爱给网_aigei_com.gif");
    // 连接登录成功的信号
    connect(m_loginWidget, &LoginOrSignUpWidget::loginSuccessfully,
            this, &AppController::handleLoginSuccess);

}

AppController::~AppController() {
    if(m_loginWidget)
        delete m_loginWidget;
    if(m_mainWindow)
        delete m_mainWindow;
}

void AppController::start()
{
    m_loginWidget->show(); // 初始显示登录窗口
}

void AppController::handleLoginSuccess()
{
    m_loginWidget->hide(); // 隐藏登录窗口

    HotkeyMapper::GetInstance()->loadConfig("D:/Qt_Projects/GreatScanner/configs/ShortCut.json");

    // 创建并显示加载动画
    m_splash->show();
    QApplication::processEvents();

    m_mainWindow = new MainWindow();
    m_mainWindow->show();

    // 后续初始化...
    OcrClient::GetInstance()->init();
    if(!SqliteMgr::instance()->init("D:/Qt_Projects/GreatScanner/local_history.db")){
        qDebug() << "本地数据库连接失败(sqlite)";
    }

    m_splash->finish(m_mainWindow);
    delete m_splash;
    m_splash = nullptr;
}

void AppController::handleLogout()
{
    //暂时先不处理这个逻辑
    qDebug()<<"退出登录";
}
